//
//  ZCTabBar.m
//  BaiSi
//
//  Created by 张灿 on 16/4/25.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCTabBar.h"
#import "UIView+Frame.h"

@interface ZCTabBar ()

@property (weak, nonatomic) UIButton *button;

@end

@implementation ZCTabBar

- (UIButton *)button
{
    if (_button == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        
        [btn sizeToFit];
        
        //加载到tabbar,防止被销毁
        [self addSubview:btn];
        _button = btn;
    }
    return _button;
}


//加载自定义TabBar的时候会把原来的TabBarItem重新布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    NSInteger count = self.items.count;
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.bounds.size.width / (count + 1);
    CGFloat h = self.bounds.size.height;
    
    int i = 0;
    
    for (UIView *button in self.subviews) {
        if ([button isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i == 2) {
                i += 1;
            }
            x = i * w;
            
            button.frame = CGRectMake(x, y, w, h);
            
            i++;
        }
    }
    
    //设置自定义Button的位置
    self.button.centerX = self.bounds.size.width * 0.5;
    self.button.centerY = self.bounds.size.height * 0.5;
    
    
}



@end
